package com.zzw.thread;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class SimpleThreadLocal<V> {
    private Map valueMap = Collections.synchronizedMap(new HashMap<Thread, V>());
    public void set(V newValue){
        valueMap.put(Thread.currentThread(),newValue);
    }

    public V get(){
        Thread currendThread = Thread.currentThread();
        V v = (V) valueMap.get(currendThread);
        if(v == null && valueMap.containsKey(currendThread)){
            v = initialValue();
            valueMap.put(currendThread,v);
        }
        return v;
    }

    public void remove(){
        valueMap.remove(Thread.currentThread());
    }

    private V initialValue() {
        return null;
    }
}
